//
//  WDGoutPendingVehicleViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/16.
//

#import "WDGoutPendingVehicleViewController.h"
#import "WDGoutPendingVehicleViewModel.h"
#import "WDWorderOrderWaitVehicleTopView.h"
#import "WDWorderOrderWaitVehicleBottomView.h"
#import "TYZPlaceholderTextView.h"

@interface WDGoutPendingVehicleViewController () <UITextViewDelegate>

@property (nonatomic, strong) WDGoutPendingVehicleViewModel *viewModel;

@property (nonatomic, strong) WDWorderOrderWaitVehicleTopView *topView;

@property (nonatomic, strong) UIView *topSectionView;

/// 处理说明标题
@property (nonatomic, strong) UILabel *remarkLabel;

/// 处理说明输入框
@property (nonatomic, strong) TYZPlaceholderTextView *remarkView;


@property (nonatomic, strong) WDWorderOrderWaitVehicleBottomView *bottomView;

@end

@implementation WDGoutPendingVehicleViewController

- (void)dealloc {
    debugMethod();
    
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDGoutPendingVehicleViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    self.rootContent.height = self.rootContent.height - TABBAR_HEIGHT;
    
    [self initWithTopView];
    
    [self initWithTopSectionView];
    
    [self initWithBottomView];
    
    if ([[EDSAppDelegateTool sharedInstance] hasPermission:@(10020502) parentCode:@(100205)]) {
        /// 处理说明标题
        [self initWithRemarkLabel];

        /// 处理说明输入框
        [self initWithRemarkView];
    }
    
    

}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDWorderOrderWaitVehicleTopViewHeight);
        _topView = [[WDWorderOrderWaitVehicleTopView alloc] initWithFrame:frame];
        _topView.hiddenBottomLine = YES;
        _topView.hiddenApplyCancel = YES;
        [self.rootContent addSubview:_topView];
        
        @weakify(self);
        // 详情
        _topView.touchDetailBlock = ^{
            @strongify(self);
            [self actionWithDetail];
        };
    }
    [_topView updateWithViewData:self.viewModel.serviceModel];
}

- (void)initWithTopSectionView {
    if (!_topSectionView) {
        CGRect frame = CGRectMake(0, self.topView.bottom, kScreenW, 10);
        _topSectionView = [[UIView alloc] initWithFrame:frame];
        _topSectionView.backgroundColor  = [UIColor colorWithHexString:@"#F8F8F8"];
        [self.rootContent addSubview:_topSectionView];
    }
}


/// 处理说明标题
- (void)initWithRemarkLabel {
    if (!_remarkLabel) {
        CGRect frame = CGRectMake(20, self.topSectionView.bottom + 10, kScreenW - 40, 20);
        _remarkLabel = [UILabel createWithLabel:self.rootContent frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"备注说明";
            label.textColor = [UIColor colorWithHexString:@"#000000"];
        }];
    }
}

/// 处理说明输入框
- (void)initWithRemarkView {
    if (!_remarkView) {
        CGRect frame = CGRectMake(20, self.remarkLabel.bottom + 12, self.remarkLabel.width, 130);
        _remarkView = [[TYZPlaceholderTextView alloc] initWithFrame:frame];
        _remarkView.font = FONT_Regular(13);
        _remarkView.delegate = self;
        _remarkView.clipsToBounds = YES;
        _remarkView.textColor = UIColorFromHex(0x000000, 1.0);
        _remarkView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        _remarkView.textContainer.lineFragmentPadding = 0;
        _remarkView.textContainerInset = UIEdgeInsetsMake(15, 9, 15, 9);
        _remarkView.placeholder = @"请输入文字，不少于20汉字；";
        _remarkView.placeholderFont = FONT_Regular(13);
        _remarkView.placeholderColor = [UIColor colorWithHexString:@"#BBBBBB"];
        _remarkView.layer.borderColor = [UIColor colorWithHexString:@"#DDDDDD"].CGColor;
        _remarkView.layer.borderWidth = 1;
        _remarkView.layer.cornerRadius = 8;
        _remarkView.layer.masksToBounds = YES;
        [self.rootContent addSubview:_remarkView];
        RAC(self.viewModel.reqModel, workExplain) = self.remarkView.rac_textSignal;
    }
}

- (void)initWithBottomView {
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020502) parentCode:@(100205)]) {
        return;
    }
    if (!_bottomView) {
        CGRect frame = CGRectMake(0, self.rootContent.bottom, kScreenW, TABBAR_HEIGHT);
        _bottomView = [[WDWorderOrderWaitVehicleBottomView alloc] initWithFrame:frame];
        [self.view addSubview:_bottomView];
        
        @weakify(self);
        _bottomView.touchScanCodeBlock = ^{
            @strongify(self);
            [self actionWithStartMaintain];
        };
    }
    _bottomView.btnTitle = @"提交报单";
}

/// @"提交报单
- (void)actionWithStartMaintain {
    NSString *workExplain = objectNull(self.viewModel.reqModel.workExplain);
    if (workExplain.length > 200) {
        [SVProgressHUD showInfoWithStatus:@"备注说明不能超过200个字！"];
        return;
    }

    [SVProgressHUD showInfoWithStatus:@"报单提交中..."];
    @weakify(self);
    [self.viewModel requestWithSubmitReport:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [SVProgressHUD showSuccessWithStatus:@"提交成功！"];
            if (self.popResultBlock) {
                self.popResultBlock(nil);
            }
            [self clickedBack:nil];
        } else {
            NSString *tip = objectNull(respond.msg);
            if ([tip isEqualToString:@""]) {
                tip = @"提交失败，请稍后再试！";
            }
            [SVProgressHUD showInfoWithStatus:tip];
        }
    }];
}

/// 工单详情
- (void)actionWithDetail {
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020501) parentCode:@(100205)]) {
        [SVProgressHUD showInfoWithStatus:@"您没有查看详情的权限！"];
        return;
    }
    
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
//            [self doRefreshData];
    };
    params[@"block"] = block;
    
    UIViewController *vc = [[TYZRouter sharedInstance] getGoutPendingVehicleDetailVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
//    CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(kRealValue(343), CGFLOAT_MAX)];
//    CGFloat heigth = sizeToFit.height;
//    [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.detailAddressLabel.mas_bottom).offset(kRealValue(14.5));
//        make.height.mas_equalTo(heigth);
//        make.width.mas_equalTo(kRealValue(343));
//        make.left.equalTo(self.detailAddressLabel.mas_left);
//        make.bottom.equalTo(self.detailAddressView.mas_bottom).offset(-kRealValue(12));
//    }];
}
@end
